# -*- coding:utf-8 -*-

# from werkzeug.routing import BaseConverter
#
# class RegexConverter(BaseConverter):
#     '''自定义的正则转换器'''
#
#     def __init__(self,url_map,*args):
#         super(RegexConverter,self).__init__(url_map)
#         self.regex = args[0]


from werkzeug.routing import BaseConverter
from flask import session,jsonify,g
from ihome.utils.response_code import RET
import functools




class RegexConverter(BaseConverter):
    """自定义的正则转换器"""

    def __init__(self, url_map, *args):
        super(RegexConverter, self).__init__(url_map)
        self.regex = args[0]

def login_required(f):
    #被装饰装饰的函数会默认被修改其__name__属性
    @functools.wraps(f)    #防止被修饰的函数的__name__被修改的问题
    def wrapper(*args,**kwargs):
        # 检测登录状态
        user_id = session.get('user_id')
        #如果不存在,也就是没登录
        if not user_id:
            return jsonify(errno = RET.SESSIONERR,errmsg='用户为登陆')
        #存在忽的话
        else:
            g.user_id = user_id
            #执行所修饰的函数并返回其响应值
            return f(*args,**kwargs)
    return wrapper